<template>
    <div class="login-bg-container">
        <!-- 食品溯源主题背景 -->
        <div class="food-traceability-bg">
            <!-- 主背景渐变 -->
            <div class="main-gradient"></div>

            <!-- 麻辣兔头相关装饰元素 -->
            <div class="food-elements">
                <!-- 兔头相关图标 -->
                <div class="food-icon rabbit-1">🐰</div>
                <div class="food-icon rabbit-2">🐇</div>
                <div class="food-icon rabbit-3">🍖</div>
                <div class="food-icon rabbit-4">🥩</div>

                <!-- 麻辣调料图标 -->
                <div class="food-icon spice-1">🌶️</div>
                <div class="food-icon spice-2">🌶</div>
                <div class="food-icon spice-3">🧄</div>
                <div class="food-icon spice-4">🧅</div>

                <!-- 四川特色元素 -->
                <div class="food-icon sichuan-1">🌶️</div>
                <div class="food-icon sichuan-2">🥢</div>
                <div class="food-icon sichuan-3">🍲</div>

                <!-- 传统食品图标 -->
                <div class="food-icon traditional-1">🥘</div>
                <div class="food-icon traditional-2">🍜</div>
                <div class="food-icon traditional-3">🥟</div>
            </div>

            <!-- 数据流线条 -->
            <div class="data-flow-lines">
                <div class="flow-line line-1"></div>
                <div class="flow-line line-2"></div>
                <div class="flow-line line-3"></div>
                <div class="flow-line line-4"></div>
            </div>

            <!-- 二维码装饰 -->
            <div class="qr-decoration">
                <div class="qr-code qr-1"></div>
                <div class="qr-code qr-2"></div>
                <div class="qr-code qr-3"></div>
            </div>

            <!-- 扫描线效果 -->
            <div class="scan-lines">
                <div class="scan-line scan-1"></div>
                <div class="scan-line scan-2"></div>
                <div class="scan-line scan-3"></div>
            </div>
        </div>

        <!-- 登录卡片 -->
        <div class="login-card-container">
            <el-card class="login-card">
                <!-- 系统标题 -->
                <div class="system-title">
                    <div class="title-icon">🐰</div>
                    <h1>溯源系统</h1>
                    <p>正宗川味，品质可追溯</p>
                </div>

                <el-form ref="ruleFormRef" class="login-form" :model="ruleForm" :rules="rules" label-width="auto">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleForm.username" placeholder="请输入用户名" prefix-icon="User" size="large" />
                    </el-form-item>

                    <el-form-item label="密码" prop="pwd">
                        <el-input v-model="ruleForm.pwd" type="password" show-password placeholder="请输入密码"
                            prefix-icon="Lock" size="large" />
                    </el-form-item>

                    <el-form-item label="验证码" prop="checkCode">
                        <div class="verification-container">
                            <el-input v-model="ruleForm.checkCode" placeholder="请输入验证码" size="large"
                                style="width: 120px;" />
                            <el-tag type="primary" size="large" class="verification-code">
                                {{ ruleForm.showCode }}=?
                            </el-tag>
                            <el-link type="primary" @click="createCheckCode" class="refresh-link">
                                看不清,换一张
                            </el-link>
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-checkbox v-model="ruleForm.savePassword" label="记住密码" size="large" />
                    </el-form-item>

                    <el-form-item class="button-group">
                        <el-button type="primary" size="large" class="login-btn" @click="submitForm(ruleFormRef)">
                            登录
                        </el-button>
                        <el-button @click="resetForm(ruleFormRef)" size="large" class="reset-btn">
                            重置
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import httpaxios from '@/Httpaxios/Httpaxios'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
//import axios from 'axios'
import { useRouter } from 'vue-router'
//引入pinia
import { useCounterStore } from "@/stores/counter"
//pinia 变量
const counter = useCounterStore();
const router = useRouter()


//钩子函数
onMounted(() => {
    createCheckCode()
})

//登录类型
interface RuleForm {
    username: string
    pwd: string
    checkCode: string //输入验证码
    showCode: string  //显示验证码(生成)
    savePassword: boolean //记住密码
}

const ruleFormRef = ref<FormInstance>()
//用户登录
const ruleForm = reactive<RuleForm>({
    username: '',
    pwd: '',
    checkCode: '', //输入验证码
    showCode: '',  //显示验证码(生成)
    savePassword: false //记住密码

})

const rules = reactive<FormRules<RuleForm>>({
    username: [
        // { required: true, message: '请输入用户名', trigger: 'blur' },
        // { min: 3, max: 30, message: '字符长度介于[3-30]', trigger: 'blur' },
    ],
    pwd: [
        // { required: true, message: '请输入密码', trigger: 'blur' },
        // { min: 4, max: 30, message: '字符长度介于[4-30]', trigger: 'blur' },
    ],
    checkCode: [
        // { required: true, message: '请输入验证码', trigger: 'blur' },
        //{ min: 4, max: 30, message: '字符长度介于[4-30]', trigger: 'blur' },
    ],
})
//生成验证码
const createCheckCode = () => {
    //数据源
    const strCode1 = "0123456789"
    const strCode2 = "0123456789qwertyuiopasdfghjASDFGHJKQLMNVCXZSTY"
    //运算符
    const strCalc = "+-*/"

    //数字+字母
    //清除已经生成的验证码
    ruleForm.showCode = "";
    //1.生成4位 循环四次
    // for (let i = 0; i < 4; i++)
    //  {
    //   //随机下标
    //   const index = Math.floor(Math.random() * strCode2.length);
    //   //获取下标对应的字符,追加到生成的验证码中
    //   ruleForm.showCode = ruleForm.showCode + strCode2[index];
    //  }
    //2.生成算式验证码
    //第一个数字
    let num1 = Math.floor(Math.random() * 9) + 1;
    //第二个数字
    let num2 = Math.floor(Math.random() * 9) + 1;
    //运算符号
    const calc = strCalc[Math.floor(Math.random() * strCalc.length)];
    //减法小于0
    if (calc == "-" && (num1 - num2 < 0)) {
        const temp = num1;
        num1 = num2;
        num2 = temp;
    }
    //除法,除不尽
    if (calc == "/" && (num1 % num2 > 0)) {
        num1 = num1 * num2;
    }
    //生成验证码
    ruleForm.showCode = num1 + calc + num2;
}
//登
//登录
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!', ruleForm)
            //验证码判断,不区分大小写(转换为统一大小写)
            // if (ruleForm.checkCode.toUpperCase() != ruleForm.showCode.toUpperCase())
            // {
            //   ElMessage.error("验证码错误!请重新输入")
            //   createCheckCode()
            //   return
            // }

            //算式判断
            if (ruleForm.checkCode != eval(ruleForm.showCode).toString()) {
                ElMessage.error("验证码错误!请重新输入")
                createCheckCode()
                return
            }

            httpaxios.post('http://localhost:5002/api/User/Login', ruleForm).then(res => {
                if (res.data.code == 100) {
                    //        // 登录成功，写JWT的 token到本地
                    // window.localStorage.setItem("token", res.data.accessToken);
                    //       // 数据提交后端要加入新的Token值
                    // axios.defaults.headers.common["Authorization"] =
                    //         "Bearer " + res.data.accessToken;
                    ElMessage.success("登录成功!")
                    //
                    router.push('/Main')
                }
                else {
                    ElMessage.error(res.data.message)
                }

            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}


</script>
<style scoped>
.login-bg-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    overflow: hidden;
}

/* 食品溯源主题背景 */
.food-traceability-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
}

/* 主背景渐变 - 柔和的食品溯源主题 */
.main-gradient {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg,
            #2d5a27 0%,
            #4a7c59 20%,
            #6b8e23 40%,
            #8fbc8f 60%,
            #a8d8a8 80%,
            #c8e6c8 100%);
    background-attachment: fixed;
}

/* 食品图标装饰 */
.food-elements {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.food-icon {
    position: absolute;
    font-size: 2.5rem;
    opacity: 0.15;
    animation: foodFloat 8s ease-in-out infinite;
    filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.3));
}

/* 兔头相关图标位置 */
.rabbit-1 {
    top: 15%;
    left: 8%;
    animation-delay: 0s;
}

.rabbit-2 {
    top: 25%;
    right: 12%;
    animation-delay: 1s;
}

.rabbit-3 {
    bottom: 30%;
    left: 15%;
    animation-delay: 2s;
}

.rabbit-4 {
    top: 60%;
    right: 8%;
    animation-delay: 3s;
}

/* 麻辣调料图标位置 */
.spice-1 {
    top: 20%;
    left: 85%;
    animation-delay: 0.5s;
}

.spice-2 {
    top: 45%;
    left: 5%;
    animation-delay: 1.5s;
}

.spice-3 {
    bottom: 25%;
    right: 20%;
    animation-delay: 2.5s;
}

.spice-4 {
    top: 70%;
    left: 25%;
    animation-delay: 3.5s;
}

/* 四川特色图标位置 */
.sichuan-1 {
    top: 35%;
    right: 5%;
    animation-delay: 1s;
}

.sichuan-2 {
    bottom: 40%;
    left: 80%;
    animation-delay: 2s;
}

.sichuan-3 {
    top: 80%;
    right: 35%;
    animation-delay: 3s;
}

/* 传统食品图标位置 */
.traditional-1 {
    top: 10%;
    left: 50%;
    animation-delay: 0.8s;
}

.traditional-2 {
    bottom: 15%;
    left: 45%;
    animation-delay: 1.8s;
}

.traditional-3 {
    top: 50%;
    right: 30%;
    animation-delay: 2.8s;
}

/* 数据流线条 */
.data-flow-lines {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.flow-line {
    position: absolute;
    background: linear-gradient(90deg,
            transparent 0%,
            rgba(255, 255, 255, 0.3) 50%,
            transparent 100%);
    height: 2px;
    animation: dataFlow 6s linear infinite;
}

.line-1 {
    top: 20%;
    left: -100%;
    width: 200px;
    animation-delay: 0s;
}

.line-2 {
    top: 40%;
    right: -100%;
    width: 150px;
    animation-delay: 2s;
}

.line-3 {
    top: 60%;
    left: -100%;
    width: 180px;
    animation-delay: 4s;
}

.line-4 {
    top: 80%;
    right: -100%;
    width: 120px;
    animation-delay: 1s;
}

/* 二维码装饰 */
.qr-decoration {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.qr-code {
    position: absolute;
    width: 40px;
    height: 40px;
    background:
        linear-gradient(45deg,
            rgba(255, 255, 255, 0.1) 25%,
            transparent 25%,
            transparent 75%,
            rgba(255, 255, 255, 0.1) 75%),
        linear-gradient(-45deg,
            rgba(255, 255, 255, 0.1) 25%,
            transparent 25%,
            transparent 75%,
            rgba(255, 255, 255, 0.1) 75%);
    background-size: 8px 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    animation: qrPulse 4s ease-in-out infinite;
}

.qr-1 {
    top: 30%;
    left: 10%;
    animation-delay: 0s;
}

.qr-2 {
    top: 60%;
    right: 15%;
    animation-delay: 1.5s;
}

.qr-3 {
    bottom: 20%;
    left: 70%;
    animation-delay: 3s;
}

/* 扫描线效果 */
.scan-lines {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.scan-line {
    position: absolute;
    width: 100%;
    height: 1px;
    background: linear-gradient(90deg,
            transparent 0%,
            rgba(0, 255, 0, 0.8) 50%,
            transparent 100%);
    animation: scanMove 3s linear infinite;
}

.scan-1 {
    top: 25%;
    animation-delay: 0s;
}

.scan-2 {
    top: 50%;
    animation-delay: 1s;
}

.scan-3 {
    top: 75%;
    animation-delay: 2s;
}

/* 动画定义 */
@keyframes foodFloat {

    0%,
    100% {
        transform: translateY(0px) rotate(0deg);
        opacity: 0.15;
    }

    50% {
        transform: translateY(-15px) rotate(5deg);
        opacity: 0.25;
    }
}

@keyframes dataFlow {
    0% {
        transform: translateX(0);
        opacity: 0;
    }

    50% {
        opacity: 1;
    }

    100% {
        transform: translateX(100vw);
        opacity: 0;
    }
}

@keyframes qrPulse {

    0%,
    100% {
        transform: scale(1);
        opacity: 0.3;
    }

    50% {
        transform: scale(1.1);
        opacity: 0.6;
    }
}

@keyframes scanMove {
    0% {
        transform: translateX(-100%);
        opacity: 0;
    }

    50% {
        opacity: 1;
    }

    100% {
        transform: translateX(100%);
        opacity: 0;
    }
}

/* 登录卡片容器 */
.login-card-container {
    position: relative;
    z-index: 10;
    width: 100%;
    max-width: 480px;
    padding: 20px;
}

/* 登录卡片样式 - 麻辣兔头主题 */
.login-card {
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    box-shadow:
        0 25px 50px rgba(45, 90, 39, 0.15),
        0 0 0 1px rgba(255, 255, 255, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.4);
    border: 2px solid rgba(76, 124, 89, 0.2);
    padding: 50px 40px;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

.login-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg,
            #2d5a27 0%,
            #4a7c59 25%,
            #6b8e23 50%,
            #8fbc8f 75%,
            #a8d8a8 100%);
}

.login-card:hover {
    transform: translateY(-8px) scale(1.02);
    box-shadow:
        0 35px 70px rgba(45, 90, 39, 0.2),
        0 0 0 1px rgba(255, 255, 255, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

/* 系统标题 - 麻辣兔头主题 */
.system-title {
    text-align: center;
    margin-bottom: 45px;
    position: relative;
}

.title-icon {
    font-size: 3rem;
    margin-bottom: 15px;
    display: block;
    animation: iconPulse 2s ease-in-out infinite;
}

.system-title h1 {
    font-size: 36px;
    font-weight: 800;
    background: linear-gradient(135deg,
            #2d5a27 0%,
            #4a7c59 30%,
            #6b8e23 60%,
            #8fbc8f 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin: 0 0 15px 0;
    letter-spacing: 2px;
    text-shadow: 0 2px 4px rgba(139, 0, 0, 0.1);
}

.system-title p {
    color: #4a7c59;
    font-size: 16px;
    margin: 0;
    font-weight: 500;
    letter-spacing: 1px;
    opacity: 0.8;
}

/* 标题图标动画 */
@keyframes iconPulse {

    0%,
    100% {
        transform: scale(1);
        filter: drop-shadow(0 0 5px rgba(220, 20, 60, 0.3));
    }

    50% {
        transform: scale(1.1);
        filter: drop-shadow(0 0 10px rgba(220, 20, 60, 0.5));
    }
}

/* 表单样式 - 麻辣兔头主题 */
.login-form {
    width: 100%;
}

.login-form .el-form-item {
    margin-bottom: 28px;
}

.login-form .el-form-item__label {
    font-weight: 700;
    color: #8B0000;
    font-size: 16px;
    margin-bottom: 8px;
    display: block;
}

/* 输入框样式 - 麻辣兔头主题 */
.login-form .el-input__wrapper {
    border-radius: 16px;
    box-shadow:
        0 4px 12px rgba(139, 0, 0, 0.08),
        inset 0 1px 0 rgba(255, 255, 255, 0.6);
    border: 2px solid rgba(220, 20, 60, 0.2);
    background: rgba(255, 255, 255, 0.9);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    height: 52px;
}

.login-form .el-input__wrapper:hover {
    border-color: #DC143C;
    box-shadow:
        0 6px 20px rgba(220, 20, 60, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transform: translateY(-1px);
}

.login-form .el-input__wrapper.is-focus {
    border-color: #FF4500;
    box-shadow:
        0 8px 25px rgba(255, 69, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.9);
    transform: translateY(-2px);
}

.login-form .el-input__inner {
    color: #8B0000;
    font-weight: 500;
    font-size: 15px;
}

.login-form .el-input__inner::placeholder {
    color: #FF6347;
    font-weight: 400;
}

/* 输入框图标样式 */
.login-form .el-input__prefix {
    color: #DC143C;
}

.login-form .el-input__suffix {
    color: #DC143C;
}

/* 验证码容器 - 麻辣兔头主题 */
.verification-container {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.verification-code {
    background: linear-gradient(135deg,
            #8B0000 0%,
            #DC143C 50%,
            #FF4500 100%);
    border: none;
    color: white;
    font-weight: 700;
    border-radius: 12px;
    padding: 10px 16px;
    min-width: 90px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(139, 0, 0, 0.3);
    transition: all 0.3s ease;
}

.verification-code:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(139, 0, 0, 0.4);
}

.refresh-link {
    font-size: 13px;
    text-decoration: none;
    color: #DC143C;
    font-weight: 500;
    transition: all 0.3s ease;
    padding: 4px 8px;
    border-radius: 6px;
}

.refresh-link:hover {
    color: #8B0000;
    background: rgba(220, 20, 60, 0.1);
    transform: translateY(-1px);
}

/* 按钮组 - 麻辣兔头主题 */
.button-group {
    margin-top: 35px;
    text-align: center;
    display: flex;
    justify-content: center;
    gap: 20px;
    flex-wrap: wrap;
}

.button-group .el-button {
    width: 140px;
    height: 50px;
    border-radius: 16px;
    font-weight: 700;
    font-size: 16px;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    margin: 0;
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
}

.login-btn {
    background: linear-gradient(135deg,
            #8B0000 0%,
            #DC143C 50%,
            #FF4500 100%);
    border: none;
    color: white;
    box-shadow: 0 6px 20px rgba(139, 0, 0, 0.3);
}

.login-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg,
            transparent,
            rgba(255, 255, 255, 0.2),
            transparent);
    transition: left 0.5s;
}

.login-btn:hover::before {
    left: 100%;
}

.login-btn:hover {
    background: linear-gradient(135deg,
            #660000 0%,
            #B22222 50%,
            #CC3300 100%);
    transform: translateY(-3px);
    box-shadow: 0 10px 30px rgba(139, 0, 0, 0.4);
}

.reset-btn {
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid rgba(220, 20, 60, 0.3);
    color: #DC143C;
    box-shadow: 0 4px 12px rgba(220, 20, 60, 0.1);
}

.reset-btn:hover {
    background: rgba(220, 20, 60, 0.05);
    border-color: #DC143C;
    color: #8B0000;
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(220, 20, 60, 0.2);
}

/* 复选框样式 - 麻辣兔头主题 */
.el-checkbox {
    color: #DC143C;
    font-weight: 600;
    font-size: 15px;
}

.el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #DC143C;
    border-color: #DC143C;
    box-shadow: 0 2px 8px rgba(220, 20, 60, 0.3);
}

.el-checkbox__inner {
    border: 2px solid rgba(220, 20, 60, 0.3);
    border-radius: 6px;
    transition: all 0.3s ease;
}

.el-checkbox__inner:hover {
    border-color: #DC143C;
    box-shadow: 0 2px 8px rgba(220, 20, 60, 0.2);
}

/* 响应式设计 - 麻辣兔头主题 */
@media (max-width: 768px) {
    .login-card-container {
        padding: 15px;
    }

    .login-card {
        padding: 35px 25px;
        border-radius: 20px;
    }

    .system-title h1 {
        font-size: 30px;
        letter-spacing: 1px;
    }

    .title-icon {
        font-size: 2.5rem;
    }

    .verification-container {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .button-group {
        gap: 15px;
    }

    .button-group .el-button {
        width: 120px;
        height: 45px;
        font-size: 15px;
    }

    .food-icon {
        font-size: 2rem;
    }

    .qr-code {
        width: 30px;
        height: 30px;
    }
}

@media (max-width: 480px) {
    .login-card {
        padding: 25px 20px;
    }

    .system-title h1 {
        font-size: 26px;
    }

    .button-group {
        gap: 10px;
    }

    .button-group .el-button {
        width: 100px;
        height: 42px;
        font-size: 14px;
    }

    .login-form .el-input__wrapper {
        height: 48px;
    }
}

/* 加载动画 - 食品溯源主题 */
.login-card {
    animation: slideInUp 1s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(60px) scale(0.95);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* 额外的食品溯源主题效果 */
.login-card::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle,
            rgba(76, 124, 89, 0.03) 0%,
            transparent 70%);
    animation: ripple 8s ease-in-out infinite;
    pointer-events: none;
}

@keyframes ripple {

    0%,
    100% {
        transform: scale(1);
        opacity: 0.5;
    }

    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }
}
</style>
